﻿using DailyRssDownload.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Xml.Linq;
using System.Linq;

namespace TestDailyRssDownloadLib
{
    
    
    /// <summary>
    ///This is a test class for LogTest and is intended
    ///to contain all LogTest Unit Tests
    ///</summary>
	[TestClass()]
	public class LogTest {


		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext {
			get {
				return testContextInstance;
			}
			set {
				testContextInstance = value;
			}
		}

		[TestInitialize()]
		public void MyTestInitialize() {
			string strLogDirectory = GetLogDirectory();
			_strLogLocation = Path.Combine(strLogDirectory, "DailyRssDownloadErrors.xml");

			if (File.Exists(_strLogLocation))
				File.Delete(_strLogLocation);
		}

		//Use TestCleanup to run code after each test has run
		[TestCleanup()]
		public void MyTestCleanup() {
			if (File.Exists(_strLogLocation))
				File.Delete(_strLogLocation);
		}

		string _strLogLocation;

		/// <summary>
		///A test for LogError
		///</summary>
		[TestMethod()]
		public void LogErrorTest_Once() {
			Exception ex = new Exception("Test exception");
			Log.LogError(ex);

			Assert.AreEqual(true, File.Exists(_strLogLocation));
			XElement root = XElement.Load(_strLogLocation);
			var allElements = from e in root.DescendantNodes()
							  where e.NodeType == System.Xml.XmlNodeType.Element
							  select e as XElement;

			var elementExceptions = from e in allElements
									where e.Name == "exception"
									select e;
			Assert.AreEqual(1, elementExceptions.Count());
		}

		/// <summary>
		///A test for LogError
		///</summary>
		[TestMethod()]
		public void LogErrorTest_Twice() {
			Exception ex = new Exception("Exception1");
			Log.LogError(ex);

			ex = new Exception("Exception2");
			Log.LogError(ex);

			Assert.AreEqual(true, File.Exists(_strLogLocation));
			XElement root = XElement.Load(_strLogLocation);
			var allElements = from e in root.DescendantNodes()
							  where e.NodeType == System.Xml.XmlNodeType.Element
							  select e as XElement;

			var elementExceptions = from e in allElements
									where e.Name == "exception"
									select e;
			Assert.AreEqual(2, elementExceptions.Count());
		}

		/// <summary>
		///A test for LogError
		///</summary>
		[TestMethod()]
		public void LogErrorTest_Inner() {
			Exception exOuter = new Exception("Outer Exception", new Exception("Inner Exception"));
			Log.LogError(exOuter);

			Assert.AreEqual(true, File.Exists(_strLogLocation));
			XElement root = XElement.Load(_strLogLocation);
			var allElements = from e in root.DescendantNodes()
							  where e.NodeType == System.Xml.XmlNodeType.Element
							  select e as XElement;

			var elementExceptions = from e in allElements
									where e.Name == "exception"
									select e;
			Assert.AreEqual(2, elementExceptions.Count());
		}


		private static string GetLogDirectory() {
			try {
				Settings objSettings = new Settings();
				return objSettings.DownloadLocation;
			} catch {
				return Directory.GetCurrentDirectory();
			}
		}
	}
}
